package com.zyy.other.locator;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 类描述
 * @Author: zyy
 * @Date: 2023/08/12 10:35
 */
public class Cache {
    private List<Service> serviceList;

    public Cache() {
        serviceList = new ArrayList<>();
    }

    /**
     * 从缓存中获取服务，根据服务名
     *
     * @param serviceName
     * @return
     */
    public Service getService(String serviceName) {
        for (Service service : serviceList) {
            if (service.getName().equalsIgnoreCase(serviceName)) {
                System.out.println("get cache " + service.getName());
                return service;
            }
        }
        return null;
    }

    /**
     * 往缓存中添加服务，并判断当前服务缓存中是否已存在
     *
     * @param serviceNew
     */
    public void addService(Service serviceNew) {
        boolean exists = false;
        for (Service service : serviceList) {
            if (service.getName().equalsIgnoreCase(serviceNew.getName())) {
                exists = true;
                break;
            }
        }
        if (!exists) {
            serviceList.add(serviceNew);
        }
    }
}
